// 配置分页插件的目录
layui.extend({
    customPage: '../lib/customPage/custom-page',
    apiconfig: '../public/apiconfig'
});
layui.use(['form',"table","laypage","customPage"], function () {
    var form = layui.form,
    $ = layui.jquery;
    $(".boxLeft").height(window.screen.height-215);
    $(".boxRight").height(window.screen.height-215);
    var loading;
    //左侧的菜单信息采录
    var boxRight=function(id,code,pcode,pvalue,myvalue){
        $("body").find(".boxRight form").remove();
        $("body").find(".boxRight").append(
            '<form class="layui-form " action=""  style="width:40%;padding:5px 20px">'
            +'<table class="userDept">'
               +' <tbody>'
                    +'<tr>'
                       +'<th>所属菜单</th>'
                    +'</tr>'
                    +'<tr>'
                        +'<td>'
                            +'<div class="layui-input-block" style="margin-left:0">'
                            +'<input type="text" name="pvalue" autocomplete="off" disabled value="'+pvalue+'" lay-verify="required"  class="layui-input">'
                            +'<input type="hidden" name="pcode"  disabled value="'+pcode+'"  class="layui-input">'
                            +'</div>'
                        +'</td>'
                    +'</tr>'
                    +'<tr>'
                       +'<th>菜单名称</th>'
                    +'</tr>'
                    +'<tr>'
                        +'<td>'
                            +'<div class="layui-input-block" style="margin-left:0">'
                                +'<input type="text" name="myvalue"  autocomplete="off" value="'+myvalue+'" lay-verify="required"   class="layui-input">'
                                +'<input type="hidden" name="myid" value="'+id+'"   class="layui-input">'
                            +'</div>'
                        +'</td>'
                    +'</tr>'
                +'</tbody>'
           +'</table>'
            +'<div class="layui-form-item  submitDobBo">'
              +'<div class="layui-input-block bao-layui-input-block">'
              +'<button type="button" class="layui-btn" lay-submit="" lay-filter="submitdic">提交</button>'
              +'<button type="button" class="layui-btn bao_loginbtn remove" id="'+id+'" data-type="remove">删除</button>'
              +'</div>'
            +'</div>'
         +'</form>'
        )
        $(".remove").on("click",function(){
            var id=$(this).attr("id")
            var index=layer.alert("确定要删除吗?",function(){
                $.ajax({
                    url: 'gt/opm/dataDict', //请求地址
                    type: "DELETE", //提交的方式S
                    data:{"id":id},
                    dataType: "JSON", //返回类型 TEXT字符串 JSON XML
                    beforeSend: function() {
                        loading=layer.load(2);
                    },
                    ContentType:"application/json;charset=UTF-8",
                    success: function(data) { 
                        layer.close(index);
                        layer.closeAll('loading');
                        if(data.flag){
                            Funlist($("#userDept option:selected").val())
                            layer.msg(data.msg,{icon:1,time:2000})
                            $("body").find(".boxRight form").remove();
                        }else{
                            layer.msg(data.msg,{icon:2,time:2000})
                        }
                    }
                })
            })
        })
    }
    var Funlist=function(type){
        // 新增数据字典
        $.ajax({
            url: 'gt/opm/dataDict/query/tree', //请求地址
            type: "get", //提交的方式S
            data:{"type":type},
            dataType: "JSON", //返回类型 TEXT字符串 JSON XML
            beforeSend: function() {
                loading=layer.load(2);
            },
            ContentType:"application/json;charset=UTF-8",
            success: function(data) { 
                layer.closeAll('loading');
                var data=data.data;
                $("#fristPa li").remove();
                $.each(data,function(i,v){
                    if(type=="work_sheet_type"){
                        if(v.code=="01"){
                            $("#fristPa").append(
                                '<li>'
                                    +'<var class="titleVsr">'+v.value+'</var>'
                                    +'<div class="userDeptUlaction">'
                                        +'<img src="../images/editco.png" title="该项无法修改"   id="'+v.id+'" data-pcode="'+v.parentCode+'" data-pvalue="'+v.parentCodeValue+'" data-code="'+v.code+'" data-myvalue="'+v.value+'"  class="userDeptUlImg bao_loginbtn edituserDeptSecond " alt="" > '
                                        +'<img src="../images/addco.png" title="新增" id="" alt="" data-code="'+v.code+'"  data-myvalue="'+v.value+'" data-pcode="'+v.parentCode+'" data-pvalue="'+v.parentCodeValue+'" class="userDeptUlImg bao_loginbtn adduserDeptSecond"> '
                                    +'</div>'
                                    +'<ul class="ddsecond"></ul>'
                                +'</li>'
                            )
                            }else{
                                $("#fristPa").append(
                                    '<li>'
                                        +'<var class="titleVsr">'+v.value+'</var>'
                                        +'<div class="userDeptUlaction">'
                                            +'<img src="../images/editco.png" title="修改"  id="'+v.id+'" data-pcode="'+v.parentCode+'" data-pvalue="'+v.parentCodeValue+'" data-code="'+v.code+'" data-myvalue="'+v.value+'"  class="userDeptUlImg bao_loginbtn edituserDeptSecond " alt="" > '
                                            +'<img src="../images/addco.png" title="新增" id="" alt="" data-code="'+v.code+'"  data-myvalue="'+v.value+'" data-pcode="'+v.parentCode+'" data-pvalue="'+v.parentCodeValue+'" class="userDeptUlImg bao_loginbtn adduserDeptSecond"> '
                                        +'</div>'
                                        +'<ul class="ddsecond"></ul>'
                                    +'</li>'
                                )
                            }
                        
                        
                        if(v.childList){
                            $.each(v.childList,function(a,c){
                                $(".ddsecond").eq(i).append(
                                    '<li>'
                                        +'<var class="titleVsr">'+c.value+'</var>'
                                        +'<div class="userDeptUlaction">'
                                            +'<img src="../images/editco.png" title="修改" id="'+c.id+'" data-pcode="'+c.parentCode+'" data-pvalue="'+c.parentCodeValue+'" data-code="'+c.code+'" data-myvalue="'+c.value+'" class="userDeptUlImg bao_loginbtn edituserDeptSecond" alt="" > '
                                        +'</div>'
                                    +'</li>'
                                )
                            })
                        }
                    }else{
                        $("#fristPa").append(
                            '<li>'
                                +'<var class="titleVsr">'+v.value+'</var>'
                                +'<div class="userDeptUlaction">'
                                    +'<img src="../images/editco.png" title="修改" id="'+v.id+'" data-pcode="'+v.parentCode+'" data-pvalue="'+v.parentCodeValue+'" data-code="'+v.code+'" data-myvalue="'+v.value+'"  class="userDeptUlImg bao_loginbtn edituserDeptSecond" alt="" > '
                                +'</div>'
                                +'<ul class="ddsecond"></ul>'
                            +'</li>'
                        )
                    }
                   
                   
                })
                $(".adduserDeptSecond").on("click",function(){
                    var id=$(this).attr("id"),
                    code=$(this).data("code"),
                    myvalue=$(this).data("myvalue");
                    boxRight(id,'',code,myvalue,'')
                })
                $(".edituserDeptSecond").on("click",function(){
                    var id=$(this).attr("id"),
                    code=$(this).data("code"),
                    pcode=$(this).data("pcode"),
                    pvalue=$(this).data("pvalue"),
                    myvalue=$(this).data("myvalue");
                    if($(this).attr("title")=="该项无法修改"){
                        layer.msg("此列表字典无法更改",{icon:2,time:2000})
                    }else{
                        boxRight(id,code,pcode,pvalue,myvalue)
                    }
                    
                    
                })
            }
        })
       
       
    }
 
    // 默认字典
    Funlist($("#userDept option:selected").val())
    // 切换字典列表
    form.on('select(userDept)', function(data){
        var type=data.value;
        Funlist(type,'0');
        $("body").find(".boxRight form").remove();
    });   
    // 修改数据字典
    form.on("submit(submitdic)",function(data){
         var type=$("#userDept option:selected").val();
        // 新增
        if(data.field.myid==''){
            $.ajax({
                url: 'gt/opm/dataDict', //请求地址
                type: "post", //提交的方式S
                data:{"type":type,"fatherCode":data.field.pcode,"fatherCodeValue":data.field.pvalue,"value":data.field.myvalue},
                dataType: "JSON", //返回类型 TEXT字符串 JSON XML
                beforeSend: function() {
                    loading=layer.load(2);
                },
                ContentType:"application/json;charset=UTF-8",
                success: function(data) { 
                    layer.closeAll('loading');
                    if(data.flag){
                        Funlist(type)
                    }
                }
            })
        }else{
            $.ajax({
                url: 'gt/opm/dataDict', //请求地址
                type: "put", //提交的方式S
                data:{"id":data.field.myid,"value":data.field.myvalue},
                dataType: "JSON", //返回类型 TEXT字符串 JSON XML
                beforeSend: function() {
                    loading=layer.load(2);
                },
                ContentType:"application/json;charset=UTF-8",
                success: function(data) { 
                    layer.closeAll('loading');
                    if(data.flag){
                        Funlist($("#userDept option:selected").val())
                        layer.msg(data.msg,{icon:1,time:2000})
                    }else{
                        layer.msg(data.msg,{icon:2,time:2000})
                    }
                }
            })
        }
        
    })
    
    var active={
        adduserDept:function(){
            var type=$("#userDept option:selected").val();
            // 新增数据字典
            boxRight('','','0','所有菜单','')
        }
        ,reloaduserDept:function(){
            var type=$("#userDept option:selected").val();
            // 刷新数据字典
            Funlist(type)
        }
    };
    $("body").find(".bao_loginbtn").on('click',function(data){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    })
    $("input[type=text]").attr("maxlength",'90')
})
